package com.daxia.bumall.web.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.daxia.bumall.basemodel.CoreUser;
import com.daxia.bumall.dto.ProductCollectDTO;
import com.daxia.bumall.dto.ProductDTO;
import com.daxia.bumall.model.User;
import com.daxia.bumall.util.SpringSecurityUtils;

@Controller
@RequestMapping("productCollect")
public class ProductCollectController extends BaseController {
	
	@ResponseBody
	@RequestMapping(value = "/collect")
	public String collect(Long productId) {
		ProductDTO productDTO = new ProductDTO();
		productDTO.setId(productId);
		CoreUser currentUser = SpringSecurityUtils.getCurrentUser();
		User user = new User();
		user.setId(currentUser.getId());
		ProductCollectDTO dto = new ProductCollectDTO();
		dto.setProduct(productDTO);
		dto.setUser(user);
		List<ProductCollectDTO> list = productCollectService.find(dto, null);
		assertTrue(list == null || list.size() == 0, "该产品您已经收藏过了!");
		productCollectService.create(dto);
		return okJson();
	}
}
